package com.wenhai.concurrency.thread;

import java.util.Queue;

/**
 * @ClassName Producer
 * @Description 生产者
 * @Author 谢文海
 * @Date 2021/4/8 0:55
 * @Version 1.0
 **/
public class Producer {
    private final Queue<Integer> queue;
    private final int maxSize;
    public Producer(Queue<Integer> queue, int maxSize) {
        this.queue = queue;
        this.maxSize = maxSize;
    }

    public void supply(Integer element) {
        synchronized (queue) {
            while (queue.size() == maxSize) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                }
            }
            System.out.println("生产者生产: " + element);
            queue.add(element);
            queue.notifyAll();
        }
    }
}
